package com.example.goodpet.activity;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.widget.ViewPager2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.goodpet.R;
import com.example.goodpet.activity.Fragment.PetTipsFragment;
import com.example.goodpet.activity.Fragment.SearchResultFragment;
import com.example.goodpet.activity.Fragment.TipsContentFragment;
import com.example.goodpet.base.BaseFragment;
import com.example.goodpet.home.adapter.HomePagerAdapter;
import com.example.goodpet.utils.ActivityManager;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

public class SearchResultActivity extends FragmentActivity {

    private String TAG=this.getClass().getSimpleName();

    private ImageView iv_searchBack;
    private Button btn_search;
    private EditText et_searchText;
//    private ViewPager2 vp_search_result;
    private BaseFragment fragment;

    private String searchContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(R.color.ic_back_background)
                .light(true)
                .apply();
        ActivityManager.getInstance().addSearchResultActivity(this);

        searchContent = getIntent().getStringExtra("searchContent");
        initView();
        initFragment();

        initListener();
    }

    private void initFragment() {

        fragment=new SearchResultFragment();
        Bundle bundle=new Bundle();
        bundle.putString("searchContent",searchContent);
        /**
         * 根据type的类型决定搜索结果优先跳到哪一个结果页面
         */
        bundle.putInt("type",getIntent().getIntExtra("type",1));
        fragment.setArguments(bundle);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if(!fragment.isAdded()){
            transaction.add(R.id.fragment,fragment);
            transaction.commit();
        }
    }


    /**
     * 初始化控件
     */
    private void initView() {
        iv_searchBack=findViewById(R.id.iv_search_back);
        btn_search=findViewById(R.id.btn_search);
        et_searchText=findViewById(R.id.et_search_text);
        /**
         * 将搜索文本的内容放到编辑框
         */
        et_searchText.setText(searchContent);

        btn_search.setVisibility(View.GONE);

    }


    private void initListener() {
        iv_searchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /**
         * 监听获取焦点后，跳转到搜索页面
         */
        et_searchText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    Intent intent = new Intent(SearchResultActivity.this, SearchActivity.class);
                    intent.putExtra("searchContent",searchContent);
                    startActivity(intent);
                }
            }
        });

    }

}